package com.fanrenqiji.modules.sys.entity;

import com.fanrenqiji.common.persistence.DataEntity;
import com.google.common.collect.Lists;
import org.hibernate.annotations.*;

import javax.persistence.CascadeType;
import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author lijian E-mail:ljz0721cx@163.com
 * @version 创建时间：2015年1月22日 下午1:11:09
 * @description 区域的实体
 */
@Entity
@Table(name = "sys_area")
@DynamicInsert
@DynamicUpdate
public class Area extends DataEntity {

	private static final long serialVersionUID = 1L;

	/**
	 * 唯一编号
	 */
	private Long id;
	/**
	 * 父级编号
	 */
	private Area parent;
	/**
	 * 所有的父级编号列
	 */
	private String parentIds;
	/**
	 * 区域编号
	 */
	private String code;
	/**
	 * 区域名称
	 */
	private String name;
	/**
	 * 区域简称
	 */
	private String alias;
	/**
	 * 区域类型（1：国家；2：省份、直辖市；3：地市；4：区县）
	 */
	private String type;

	/**
	 * 部门列表
	 */
	private List<Office> officeList = Lists.newArrayList();
	/**
	 * 拥有子区域列表
	 */
	private List<Area> childList = Lists.newArrayList();

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "parent_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@NotNull
	public Area getParent() {
		return parent;
	}

	public void setParent(Area parent) {
		this.parent = parent;
	}

	@Column(name = "parent_ids")
	public String getParentIds() {
		return parentIds;
	}

	public void setParentIds(String parentIds) {
		this.parentIds = parentIds;
	}
	@Column(name = "code",unique=true)
	public String getCode() {
		return code;
	}
	@Column(name="alias",length=50)
	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}
	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE,
			CascadeType.REMOVE }, fetch = FetchType.LAZY, mappedBy = "area")
	@Where(clause = "del_flag='" + DEL_FLAG_NORMAL + "'")
	@OrderBy(value = "code")
	@NotFound(action = NotFoundAction.IGNORE)
	public List<Office> getOfficeList() {
		return officeList;
	}

	public void setOfficeList(List<Office> officeList) {
		this.officeList = officeList;
	}

	@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE,
			CascadeType.REMOVE }, fetch = FetchType.LAZY, mappedBy = "parent")
	@Where(clause = "del_flag='" + DEL_FLAG_NORMAL + "'")
	@OrderBy(value = "code")
	@NotFound(action = NotFoundAction.IGNORE)
	public List<Area> getChildList() {
		return childList;
	}

	public void setChildList(List<Area> childList) {
		this.childList = childList;
	}

	/**
	 * 排序
	 * @param list
	 * @param sourcelist
	 * @param parentId
	 */
	@Transient
	public static void sortList(List<Area> list, List<Area> sourcelist,
			Long parentId) {
		for (int i = 0; i < sourcelist.size(); i++) {
			Area e = sourcelist.get(i);
			if (e.getParent() != null && e.getParent().getId() != null
					&& e.getParent().getId().equals(parentId)) {
				list.add(e);
				// 判断是否还有子节点, 有则继续获取子节点
				for (int j = 0; j < sourcelist.size(); j++) {
					Area childe = sourcelist.get(j);
					if (childe.getParent() != null
							&& childe.getParent().getId() != null
							&& childe.getParent().getId().equals(e.getId())) {
						sortList(list, sourcelist, e.getId());
						break;
					}
				}
			}
		}
	}

	@Transient
	public boolean isAdmin() {
		return isAdmin(this.id);
	}

	@Transient
	public static boolean isAdmin(Long id) {
		return id != null && id.equals(1L);
	}

	
}
